home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / explow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-24  |  28.7 KB  |  1,041 lines  |  [TEXT/MPS ]

  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  2.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21. #include "rtl.h"
  22. #include "tree.h"
  23. #include "flags.h"
  24. #include "expr.h"
  25. #include "hard-reg-set.h"
  26. #include "insn-config.h"
  27. #include "recog.h"
  28. #include "insn-flags.h"
  29. #include "insn-codes.h"
  30.  
  31. /* Return an rtx for the sum of X and the integer C.
  32.  
  33.    This fucntion should be used via the `plus_constant' macro.  */
  34.  
  35. rtx
  36. plus_constant_wide (x, c)
  37.      register rtx x;
  38.      register HOST_WIDE_INT c;
  39. {
  40.   register RTX_CODE code;
  41.   register enum machine_mode mode;
  42.   register rtx tem;
  43.   int all_constant = 0;
  44.  
  45.   if (c == 0)
  46.     return x;
  47.  
  48.  restart:
  49.  
  50.   code = GET_CODE (x);
  51.   mode = GET_MODE (x);
  52.   switch (code)
  53.     {
  54.     case CONST_INT:
  55.       return GEN_INT (INTVAL (x) + c);
  56.  
  57.     case CONST_DOUBLE:
  58.       {
  59.     HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
  60.     HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
  61.     HOST_WIDE_INT l2 = c;
  62.     HOST_WIDE_INT h2 = c < 0 ? ~0 : 0;
  63.     HOST_WIDE_INT lv, hv;
  64.  
  65.     add_double (l1, h1, l2, h2, &lv, &hv);
  66.  
  67.     return immed_double_const (lv, hv, VOIDmode);
  68.       }
  69.  
  70.     case MEM:
  71.       /* If this is a reference to the constant pool, try replacing it with
  72.      a reference to a new constant.  If the resulting address isn't
  73.      valid, don't return it because we have no way to validize it.  */
  74.       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
  75.       && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
  76.     {
  77.       tem
  78.         = force_const_mem (GET_MODE (x),
  79.                    plus_constant (get_pool_constant (XEXP (x, 0)),
  80.                           c));
  81.       if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
  82.         return tem;
  83.     }
  84.       break;
  85.  
  86.     case CONST:
  87.       /* If adding to something entirely constant, set a flag
  88.      so that we can add a CONST around the result.  */
  89.       x = XEXP (x, 0);
  90.       all_constant = 1;
  91.       goto restart;
  92.  
  93.     case SYMBOL_REF:
  94.     case LABEL_REF:
  95.       all_constant = 1;
  96. #ifdef SYMBOLS_ARE_CONSTANTS
  97.       /* Symbols and labels are not necessarily constants. */
  98.       all_constant = SYMBOLS_ARE_CONSTANTS;
  99. #endif
  100.       break;
  101.  
  102.     case PLUS:
  103.       /* The interesting case is adding the integer to a sum.
  104.      Look for constant term in the sum and combine
  105.      with C.  For an integer constant term, we make a combined
  106.      integer.  For a constant term that is not an explicit integer,
  107.      we cannot really combine, but group them together anyway.  
  108.  
  109.      Use a recursive call in case the remaining operand is something
  110.      that we handle specially, such as a SYMBOL_REF.  */
  111.  
  112.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  113.     return plus_constant (XEXP (x, 0), c + INTVAL (XEXP (x, 1)));
  114.       else if (CONSTANT_P (XEXP (x, 0)))
  115.     return gen_rtx (PLUS, mode,
  116.             plus_constant (XEXP (x, 0), c),
  117.             XEXP (x, 1));
  118.       else if (CONSTANT_P (XEXP (x, 1)))
  119.     return gen_rtx (PLUS, mode,
  120.             XEXP (x, 0),
  121.             plus_constant (XEXP (x, 1), c));
  122.     }
  123.  
  124.   if (c != 0)
  125.     x = gen_rtx (PLUS, mode, x, GEN_INT (c));
  126.  
  127.   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
  128.     return x;
  129.   else if (all_constant)
  130.     return gen_rtx (CONST, mode, x);
  131.   else
  132.     return x;
  133. }
  134.  
  135. /* This is the same as `plus_constant', except that it handles LO_SUM.
  136.  
  137.    This function should be used via the `plus_constant_for_output' macro.  */
  138.  
  139. rtx
  140. plus_constant_for_output_wide (x, c)
  141.      register rtx x;
  142.      register HOST_WIDE_INT c;
  143. {
  144.   register RTX_CODE code = GET_CODE (x);
  145.   register enum machine_mode mode = GET_MODE (x);
  146.   int all_constant = 0;
  147.  
  148.   if (GET_CODE (x) == LO_SUM)
  149.     return gen_rtx (LO_SUM, mode, XEXP (x, 0),
  150.             plus_constant_for_output (XEXP (x, 1), c));
  151.  
  152.   else
  153.     return plus_constant (x, c);
  154. }
  155.  
  156. /* If X is a sum, return a new sum like X but lacking any constant terms.
  157.    Add all the removed constant terms into *CONSTPTR.
  158.    X itself is not altered.  The result != X if and only if
  159.    it is not isomorphic to X.  */
  160.  
  161. rtx
  162. eliminate_constant_term (x, constptr)
  163.      rtx x;
  164.      rtx *constptr;
  165. {
  166.   register rtx x0, x1;
  167.   rtx tem;
  168.  
  169.   if (GET_CODE (x) != PLUS)
  170.     return x;
  171.  
  172.   /* First handle constants appearing at this level explicitly.  */
  173.   if (GET_CODE (XEXP (x, 1)) == CONST_INT
  174.       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
  175.                         XEXP (x, 1)))
  176.       && GET_CODE (tem) == CONST_INT)
  177.     {
  178.       *constptr = tem;
  179.       return eliminate_constant_term (XEXP (x, 0), constptr);
  180.     }
  181.  
  182.   tem = const0_rtx;
  183.   x0 = eliminate_constant_term (XEXP (x, 0), &tem);
  184.   x1 = eliminate_constant_term (XEXP (x, 1), &tem);
  185.   if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  186.       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
  187.                         *constptr, tem))
  188.       && GET_CODE (tem) == CONST_INT)
  189.     {
  190.       *constptr = tem;
  191.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  192.     }
  193.  
  194.   return x;
  195. }
  196.  
  197. /* Returns the insn that next references REG after INSN, or 0
  198.    if REG is clobbered before next referenced or we cannot find
  199.    an insn that references REG in a straight-line piece of code.  */
  200.  
  201. rtx
  202. find_next_ref (reg, insn)
  203.      rtx reg;
  204.      rtx insn;
  205. {
  206.   rtx next;
  207.  
  208.   for (insn = NEXT_INSN (insn); insn; insn = next)
  209.     {
  210.       next = NEXT_INSN (insn);
  211.       if (GET_CODE (insn) == NOTE)
  212.     continue;
  213.       if (GET_CODE (insn) == CODE_LABEL
  214.       || GET_CODE (insn) == BARRIER)
  215.     return 0;
  216.       if (GET_CODE (insn) == INSN
  217.       || GET_CODE (insn) == JUMP_INSN
  218.       || GET_CODE (insn) == CALL_INSN)
  219.     {
  220.       if (reg_set_p (reg, insn))
  221.         return 0;
  222.       if (reg_mentioned_p (reg, PATTERN (insn)))
  223.         return insn;
  224.       if (GET_CODE (insn) == JUMP_INSN)
  225.         {
  226.           if (simplejump_p (insn))
  227.         next = JUMP_LABEL (insn);
  228.           else
  229.         return 0;
  230.         }
  231.       if (GET_CODE (insn) == CALL_INSN
  232.           && REGNO (reg) < FIRST_PSEUDO_REGISTER
  233.           && call_used_regs[REGNO (reg)])
  234.         return 0;
  235.     }
  236.       else
  237.     abort ();
  238.     }
  239.   return 0;
  240. }
  241.  
  242. /* Return an rtx for the size in bytes of the value of EXP.  */
  243.  
  244. rtx
  245. expr_size (exp)
  246.      tree exp;
  247. {
  248.   return expand_expr (size_in_bytes (TREE_TYPE (exp)),
  249.               NULL_RTX, TYPE_MODE (sizetype), 0);
  250. }
  251.  
  252. /* Return a copy of X in which all memory references
  253.    and all constants that involve symbol refs
  254.    have been replaced with new temporary registers.
  255.    Also emit code to load the memory locations and constants
  256.    into those registers.
  257.  
  258.    If X contains no such constants or memory references,
  259.    X itself (not a copy) is returned.
  260.  
  261.    If a constant is found in the address that is not a legitimate constant
  262.    in an insn, it is left alone in the hope that it might be valid in the
  263.    address.
  264.  
  265.    X may contain no arithmetic except addition, subtraction and multiplication.
  266.    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
  267.  
  268. static rtx
  269. break_out_memory_refs (x)
  270.      register rtx x;
  271. {
  272.   if (GET_CODE (x) == MEM
  273.       || (CONSTANT_P (x) && LEGITIMATE_CONSTANT_P (x)
  274.       && GET_MODE (x) != VOIDmode))
  275.     {
  276.       register rtx temp = force_reg (GET_MODE (x), x);
  277.       mark_reg_pointer (temp);
  278.       x = temp;
  279.     }
  280.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  281.        || GET_CODE (x) == MULT)
  282.     {
  283.       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  284.       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  285.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  286.     x = gen_rtx (GET_CODE (x), DPmode, op0, op1);
  287.     }
  288.   return x;
  289. }
  290.  
  291. /* Given a memory address or facsimile X, construct a new address,
  292.    currently equivalent, that is stable: future stores won't change it.
  293.  
  294.    X must be composed of constants, register and memory references
  295.    combined with addition, subtraction and multiplication:
  296.    in other words, just what you can get from expand_expr if sum_ok is 1.
  297.  
  298.    Works by making copies of all regs and memory locations used
  299.    by X and combining them the same way X does.
  300.    You could also stabilize the reference to this address
  301.    by copying the address to a register with copy_to_reg;
  302.    but then you wouldn't get indexed addressing in the reference.  */
  303.  
  304. rtx
  305. copy_all_regs (x)
  306.      register rtx x;
  307. {
  308.   if (GET_CODE (x) == REG)
  309.     {
  310.       if (REGNO (x) != FRAME_POINTER_REGNUM)
  311.     x = copy_to_reg (x);
  312.     }
  313.   else if (GET_CODE (x) == MEM)
  314.     x = copy_to_reg (x);
  315.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  316.        || GET_CODE (x) == MULT)
  317.     {
  318.       register rtx op0 = copy_all_regs (XEXP (x, 0));
  319.       register rtx op1 = copy_all_regs (XEXP (x, 1));
  320.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  321. #if defined(APPLE_HAX) && defined(PTR_HACK)
  322.     /* JH: I think this change is true for all cases, but I'm not sure. */
  323.     x = gen_rtx (GET_CODE (x), GET_MODE (x), op0, op1);
  324. #else
  325.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  326. #endif
  327.     }
  328.   return x;
  329. }
  330.  
  331. /* Return something equivalent to X but valid as a memory address
  332.    for something of mode MODE.  When X is not itself valid, this
  333.    works by copying X or subexpressions of it into registers.  */
  334.  
  335. rtx
  336. memory_address (mode, x)
  337.      enum machine_mode mode;
  338.      register rtx x;
  339. {
  340.   register rtx oldx;
  341.  
  342.   /* By passing constant addresses thru registers
  343.      we get a chance to cse them.  */
  344.   if (! cse_not_expected && CONSTANT_P (x) && LEGITIMATE_CONSTANT_P (x))
  345. #if defined(APPLE_HAX) && defined(PTR_HACK)
  346.     return force_reg (DPmode, x); /*BP: Why not use GET_MODE?*/
  347. #else
  348.     return force_reg (Pmode, x);
  349. #endif
  350.  
  351.   /* Accept a QUEUED that refers to a REG
  352.      even though that isn't a valid address.
  353.      On attempting to put this in an insn we will call protect_from_queue
  354.      which will turn it into a REG, which is valid.  */
  355.   if (GET_CODE (x) == QUEUED
  356.       && GET_CODE (QUEUED_VAR (x)) == REG)
  357.     return x;
  358.  
  359.   /* We get better cse by rejecting indirect addressing at this stage.
  360.      Let the combiner create indirect addresses where appropriate.
  361.      For now, generate the code so that the subexpressions useful to share
  362.      are visible.  But not if cse won't be done!  */
  363.   oldx = x;
  364.   if (! cse_not_expected && GET_CODE (x) != REG)
  365.     x = break_out_memory_refs (x);
  366.  
  367.   /* At this point, any valid address is accepted.  */
  368.   GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  369.  
  370.   /* If it was valid before but breaking out memory refs invalidated it,
  371.      use it the old way.  */
  372.   if (memory_address_p (mode, oldx))
  373.     goto win2;
  374.  
  375.   /* Perform machine-dependent transformations on X
  376.      in certain cases.  This is not necessary since the code
  377.      below can handle all possible cases, but machine-dependent
  378.      transformations can make better code.  */
  379.   LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  380.  
  381.   /* PLUS and MULT can appear in special ways
  382.      as the result of attempts to make an address usable for indexing.
  383.      Usually they are dealt with by calling force_operand, below.
  384.      But a sum containing constant terms is special
  385.      if removing them makes the sum a valid address:
  386.      then we generate that address in a register
  387.      and index off of it.  We do this because it often makes
  388.      shorter code, and because the addresses thus generated
  389.      in registers often become common subexpressions.  */
  390.   if (GET_CODE (x) == PLUS)
  391.     {
  392.       rtx constant_term = const0_rtx;
  393.       rtx y = eliminate_constant_term (x, &constant_term);
  394.       if (constant_term == const0_rtx
  395.       || ! memory_address_p (mode, y))
  396.     return force_operand (x, NULL_RTX);
  397.  
  398.       y = gen_rtx (PLUS, GET_MODE (x), copy_to_reg (y), constant_term);
  399.       if (! memory_address_p (mode, y))
  400.     return force_operand (x, NULL_RTX);
  401.       return y;
  402.     }
  403.   if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  404.     return force_operand (x, NULL_RTX);
  405.  
  406.   /* If we have a register that's an invalid address,
  407.      it must be a hard reg of the wrong class.  Copy it to a pseudo.  */
  408.   if (GET_CODE (x) == REG)
  409.     return copy_to_reg (x);
  410.  
  411.   /* Last resort: copy the value to a register, since
  412.      the register is a valid address.  */
  413.   if (GET_MODE (x) == DPmode)
  414.     return force_reg (DPmode, x);
  415.   else
  416.     return force_reg (TPmode, x);
  417.  win2:
  418.   x = oldx;
  419.  win:
  420.   if (flag_force_addr && ! cse_not_expected && GET_CODE (x) != REG
  421.       /* Don't copy an addr via a reg if it is one of our stack slots.  */
  422.       && ! (GET_CODE (x) == PLUS
  423.         && (XEXP (x, 0) == virtual_stack_vars_rtx
  424.         || XEXP (x, 0) == virtual_incoming_args_rtx)))
  425.     {
  426.       if (general_operand (x, DPmode))
  427.     return force_reg (DPmode, x);
  428.       else
  429.     return force_operand (x, NULL_RTX);
  430.     }
  431.   return x;
  432. }
  433.  
  434. /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
  435.  
  436. rtx
  437. memory_address_noforce (mode, x)
  438.      enum machine_mode mode;
  439.      rtx x;
  440. {
  441.   int ambient_force_addr = flag_force_addr;
  442.   rtx val;
  443.  
  444.   flag_force_addr = 0;
  445.   val = memory_address (mode, x);
  446.   flag_force_addr = ambient_force_addr;
  447.   return val;
  448. }
  449.  
  450. /* Convert a mem ref into one with a valid memory address.
  451.    Pass through anything else unchanged.  */
  452.  
  453. rtx
  454. validize_mem (ref)
  455.      rtx ref;
  456. {
  457.   if (GET_CODE (ref) != MEM)
  458.     return ref;
  459.   if (memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
  460.     return ref;
  461.   /* Don't alter REF itself, since that is probably a stack slot.  */
  462.   return change_address (ref, GET_MODE (ref), XEXP (ref, 0));
  463. }
  464.  
  465. /* Return a modified copy of X with its memory address copied
  466.    into a temporary register to protect it from side effects.
  467.    If X is not a MEM, it is returned unchanged (and not copied).
  468.    Perhaps even if it is a MEM, if there is no need to change it.  */
  469.  
  470. rtx
  471. stabilize (x)
  472.      rtx x;
  473. {
  474.   register rtx addr;
  475.   if (GET_CODE (x) != MEM)
  476.     return x;
  477.   addr = XEXP (x, 0);
  478.   if (rtx_unstable_p (addr))
  479.     {
  480.       rtx temp = copy_all_regs (addr);
  481.       rtx mem;
  482.       if (GET_CODE (temp) != REG)
  483.     temp = copy_to_reg (temp);
  484.       mem = gen_rtx (MEM, GET_MODE (x), temp);
  485.  
  486.       /* Mark returned memref with in_struct if it's in an array or
  487.      structure.  Copy const and volatile from original memref.  */
  488.  
  489.       MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (x) || GET_CODE (addr) == PLUS;
  490.       RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (x);
  491.       MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (x);
  492.       return mem;
  493.     }
  494.   return x;
  495. }
  496.  
  497. /* Copy the value or contents of X to a new temp reg and return that reg.  */
  498.  
  499. rtx
  500. copy_to_reg (x)
  501.      rtx x;
  502. {
  503.   register rtx temp = gen_reg_rtx (GET_MODE (x));
  504.  
  505.   /* If not an operand, must be an address with PLUS and MULT so
  506.      do the computation.  */ 
  507.   if (! general_operand (x, VOIDmode))
  508.     x = force_operand (x, temp);
  509.   
  510.   if (x != temp)
  511.     emit_move_insn (temp, x);
  512.  
  513.   return temp;
  514. }
  515.  
  516. /* Like copy_to_reg but always give the new register mode Pmode
  517.    in case X is a constant.  */
  518.  
  519. rtx
  520. copy_addr_to_reg (x)
  521.      rtx x;
  522. {
  523. #if defined(APPLE_HAX) && defined(PTR_HACK)
  524. /* BP: This one was missed by JH*/
  525. /*     I am guessing here*/
  526.   return copy_to_mode_reg (DPmode, x);
  527. #else
  528.   return copy_to_mode_reg (Pmode, x);
  529. #endif
  530. }
  531.  
  532. /* Like copy_to_reg but always give the new register mode MODE
  533.    in case X is a constant.  */
  534.  
  535. rtx
  536. copy_to_mode_reg (mode, x)
  537.      enum machine_mode mode;
  538.      rtx x;
  539. {
  540.   register rtx temp = gen_reg_rtx (mode);
  541.   
  542.   /* If not an operand, must be an address with PLUS and MULT so
  543.      do the computation.  */ 
  544.   if (! general_operand (x, VOIDmode))
  545.     x = force_operand (x, temp);
  546.  
  547.   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode) {
  548. #if 0
  549.     fprintf(stderr, "FILE %s; LINE %d\n", __FILE__, __LINE__);
  550.     fprintf(stderr, "mode=%s\n", GET_MODE_NAME(mode));
  551.     fprintf(stderr, "x=\n");
  552.     debug_rtx(x);
  553.     fprintf(stderr, "temp=\n");
  554.     debug_rtx(temp);
  555. #endif
  556.     abort();
  557.   }
  558.   if (x != temp)
  559.     emit_move_insn (temp, x);
  560.   return temp;
  561. }
  562.  
  563. /* Load X into a register if it is not already one.
  564.    Use mode MODE for the register.
  565.    X should be valid for mode MODE, but it may be a constant which
  566.    is valid for all integer modes; that's why caller must specify MODE.
  567.  
  568.    The caller must not alter the value in the register we return,
  569.    since we mark it as a "constant" register.  */
  570.  
  571. rtx
  572. force_reg (mode, x)
  573.      enum machine_mode mode;
  574.      rtx x;
  575. {
  576.   register rtx temp, insn;
  577.  
  578.   if (GET_CODE (x) == REG)
  579.     return x;
  580.   temp = gen_reg_rtx (mode);
  581.   insn = emit_move_insn (temp, x);
  582.   /* Let optimizers know that TEMP's value never changes
  583.      and that X can be substituted for it.  */
  584.   if (CONSTANT_P (x))
  585.     {
  586.       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  587.  
  588.       if (note)
  589.     XEXP (note, 0) = x;
  590.       else
  591.     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL, x, REG_NOTES (insn));
  592.     }
  593.   return temp;
  594. }
  595.  
  596. /* If X is a memory ref, copy its contents to a new temp reg and return
  597.    that reg.  Otherwise, return X.  */
  598.  
  599. rtx
  600. force_not_mem (x)
  601.      rtx x;
  602. {
  603.   register rtx temp;
  604.   if (GET_CODE (x) != MEM || GET_MODE (x) == BLKmode)
  605.     return x;
  606.   temp = gen_reg_rtx (GET_MODE (x));
  607.   emit_move_insn (temp, x);
  608.   return temp;
  609. }
  610.  
  611. /* Copy X to TARGET (if it's nonzero and a reg)
  612.    or to a new temp reg and return that reg.
  613.    MODE is the mode to use for X in case it is a constant.  */
  614.  
  615. rtx
  616. copy_to_suggested_reg (x, target, mode)
  617.      rtx x, target;
  618.      enum machine_mode mode;
  619. {
  620.   register rtx temp;
  621.  
  622.   if (target && GET_CODE (target) == REG)
  623.     temp = target;
  624.   else
  625.     temp = gen_reg_rtx (mode);
  626.  
  627.   emit_move_insn (temp, x);
  628.   return temp;
  629. }
  630.  
  631. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  632.    This pops when ADJUST is positive.  ADJUST need not be constant.  */
  633.  
  634. void
  635. adjust_stack (adjust)
  636.      rtx adjust;
  637. {
  638.   rtx temp;
  639.   adjust = protect_from_queue (adjust, 0);
  640.  
  641.   if (adjust == const0_rtx)
  642.     return;
  643.   temp = expand_binop (DPmode,
  644. #ifdef STACK_GROWS_DOWNWARD
  645.                add_optab,
  646. #else
  647.                sub_optab,
  648. #endif
  649.                stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
  650.                OPTAB_LIB_WIDEN);
  651.  
  652.   if (temp != stack_pointer_rtx)
  653.     emit_move_insn (stack_pointer_rtx, temp);
  654. }
  655.  
  656. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  657.    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
  658.  
  659. void
  660. anti_adjust_stack (adjust)
  661.      rtx adjust;
  662. {
  663.   rtx temp;
  664.   adjust = protect_from_queue (adjust, 0);
  665.  
  666.   if (adjust == const0_rtx)
  667.     return;
  668.  
  669.   temp = expand_binop (DPmode,
  670. #ifdef STACK_GROWS_DOWNWARD
  671.                sub_optab,
  672. #else
  673.                add_optab,
  674. #endif
  675.                stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
  676.                OPTAB_LIB_WIDEN);
  677.  
  678.   if (temp != stack_pointer_rtx)
  679.     emit_move_insn (stack_pointer_rtx, temp);
  680. }
  681.  
  682. /* Round the size of a block to be pushed up to the boundary required
  683.    by this machine.  SIZE is the desired size, which need not be constant.  */
  684.  
  685. rtx
  686. round_push (size)
  687.      rtx size;
  688. {
  689. #ifdef STACK_BOUNDARY
  690.   int align = STACK_BOUNDARY / BITS_PER_UNIT;
  691.   if (align == 1)
  692.     return size;
  693.   if (GET_CODE (size) == CONST_INT)
  694.     {
  695.       int new = (INTVAL (size) + align - 1) / align * align;
  696.       if (INTVAL (size) != new)
  697.     size = GEN_INT (new);
  698.     }
  699.   else
  700.     {
  701.       size = expand_divmod (0, CEIL_DIV_EXPR, DPmode, size, GEN_INT (align),
  702.                 NULL_RTX, 1);
  703.       size = expand_mult (DPmode, size, GEN_INT (align), NULL_RTX, 1);
  704.     }
  705. #endif /* STACK_BOUNDARY */
  706.   return size;
  707. }
  708.  
  709. /* Save the stack pointer for the purpose in SAVE_LEVEL.  PSAVE is a pointer
  710.    to a previously-created save area.  If no save area has been allocated,
  711.    this function will allocate one.  If a save area is specified, it
  712.    must be of the proper mode.
  713.  
  714.    The insns are emitted after insn AFTER, if nonzero, otherwise the insns
  715.    are emitted at the current position.  */
  716.  
  717. void
  718. emit_stack_save (save_level, psave, after)
  719.      enum save_level save_level;
  720.      rtx *psave;
  721.      rtx after;
  722. {
  723.   rtx sa = *psave;
  724.   /* The default is that we use a move insn and save in a Pmode object.  */
  725.   rtx (*fcn) () = gen_move_insn;
  726.   enum machine_mode mode = DPmode;
  727.  
  728.   /* See if this machine has anything special to do for this kind of save.  */
  729.   switch (save_level)
  730.     {
  731. #ifdef HAVE_save_stack_block
  732.     case SAVE_BLOCK:
  733.       if (HAVE_save_stack_block)
  734.     {
  735.       fcn = gen_save_stack_block;
  736.       mode = insn_operand_mode[CODE_FOR_save_stack_block][0];
  737.     }
  738.       break;
  739. #endif
  740. #ifdef HAVE_save_stack_function
  741.     case SAVE_FUNCTION:
  742.       if (HAVE_save_stack_function)
  743.     {
  744.       fcn = gen_save_stack_function;
  745.       mode = insn_operand_mode[CODE_FOR_save_stack_function][0];
  746.     }
  747.       break;
  748. #endif
  749. #ifdef HAVE_save_stack_nonlocal
  750.     case SAVE_NONLOCAL:
  751.       if (HAVE_save_stack_nonlocal)
  752.     {
  753.       fcn = gen_save_stack_nonlocal;
  754.       mode = insn_operand_mode[CODE_FOR_save_stack_nonlocal][0];
  755.     }
  756.       break;
  757. #endif
  758.     }
  759.  
  760.   /* If there is no save area and we have to allocate one, do so.  Otherwise
  761.      verify the save area is the proper mode.  */
  762.  
  763.   if (sa == 0)
  764.     {
  765.       if (mode != VOIDmode)
  766.     {
  767.       if (save_level == SAVE_NONLOCAL)
  768.         *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
  769.       else
  770.         *psave = sa = gen_reg_rtx (mode);
  771.     }
  772.     }
  773.   else
  774.     {
  775.       if (mode == VOIDmode || GET_MODE (sa) != mode)
  776.     abort ();
  777.     }
  778.  
  779.   if (sa != 0)
  780.     sa = validize_mem (sa);
  781.  
  782.   if (after)
  783.     {
  784.       rtx seq;
  785.  
  786.       start_sequence ();
  787.       emit_insn (fcn (sa, stack_pointer_rtx));
  788.       seq = gen_sequence ();
  789.       end_sequence ();
  790.       emit_insn_after (seq, after);
  791.     }
  792.   else
  793.     emit_insn (fcn (sa, stack_pointer_rtx));
  794. }
  795.  
  796. /* Restore the stack pointer for the purpose in SAVE_LEVEL.  SA is the save
  797.    area made by emit_stack_save.  If it is zero, we have nothing to do. 
  798.  
  799.    Put any emitted insns after insn AFTER, if nonzero, otherwise at 
  800.    current position.  */
  801.  
  802. void
  803. emit_stack_restore (save_level, sa, after)
  804.      enum save_level save_level;
  805.      rtx after;
  806.      rtx sa;
  807. {
  808.   /* The default is that we use a move insn.  */
  809.   rtx (*fcn) () = gen_move_insn;
  810.  
  811.   /* See if this machine has anything special to do for this kind of save.  */
  812.   switch (save_level)
  813.     {
  814. #ifdef HAVE_restore_stack_block
  815.     case SAVE_BLOCK:
  816.       if (HAVE_restore_stack_block)
  817.     fcn = gen_restore_stack_block;
  818.       break;
  819. #endif
  820. #ifdef HAVE_restore_stack_function
  821.     case SAVE_FUNCTION:
  822.       if (HAVE_restore_stack_function)
  823.     fcn = gen_restore_stack_function;
  824.       break;
  825. #endif
  826. #ifdef HAVE_restore_stack_nonlocal
  827.  
  828.     case SAVE_NONLOCAL:
  829.       if (HAVE_restore_stack_nonlocal)
  830.     fcn = gen_restore_stack_nonlocal;
  831.       break;
  832. #endif
  833.     }
  834.  
  835.   if (sa != 0)
  836.     sa = validize_mem (sa);
  837.  
  838.   if (after)
  839.     {
  840.       rtx seq;
  841.  
  842.       start_sequence ();
  843.       emit_insn (fcn (stack_pointer_rtx, sa));
  844.       seq = gen_sequence ();
  845.       end_sequence ();
  846.       emit_insn_after (seq, after);
  847.     }
  848.   else
  849.     emit_insn (fcn (stack_pointer_rtx, sa));
  850. }
  851.  
  852. /* Return an rtx representing the address of an area of memory dynamically
  853.    pushed on the stack.  This region of memory is always aligned to
  854.    a multiple of BIGGEST_ALIGNMENT.
  855.  
  856.    Any required stack pointer alignment is preserved.
  857.  
  858.    SIZE is an rtx representing the size of the area.
  859.    TARGET is a place in which the address can be placed.
  860.  
  861.    KNOWN_ALIGN is the alignment (in bits) that we know SIZE has.  */
  862.  
  863. rtx
  864. allocate_dynamic_stack_space (size, target, known_align)
  865.      rtx size;
  866.      rtx target;
  867.      int known_align;
  868. {
  869.   /* Ensure the size is in the proper mode.  */
  870.   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != DPmode)
  871.     size = convert_to_mode (DPmode, size, 1);
  872.  
  873.   /* We will need to ensure that the address we return is aligned to
  874.      BIGGEST_ALIGNMENT.  If STACK_DYNAMIC_OFFSET is defined, we don't
  875.      always know its final value at this point in the compilation (it 
  876.      might depend on the size of the outgoing parameter lists, for
  877.      example), so we must align the value to be returned in that case.
  878.      (Note that STACK_DYNAMIC_OFFSET will have a default non-zero value if
  879.      STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
  880.      We must also do an alignment operation on the returned value if
  881.      the stack pointer alignment is less strict that BIGGEST_ALIGNMENT.
  882.  
  883.      If we have to align, we must leave space in SIZE for the hole
  884.      that might result from the alignment operation.  */
  885.  
  886. #if defined (STACK_DYNAMIC_OFFSET) || defined(STACK_POINTER_OFFSET) || defined (ALLOCATE_OUTGOING_ARGS)
  887. #define MUST_ALIGN
  888. #endif
  889.  
  890. #if ! defined (MUST_ALIGN) && (!defined(STACK_BOUNDARY) || STACK_BOUNDARY < BIGGEST_ALIGNMENT)
  891. #define MUST_ALIGN
  892. #endif
  893.  
  894. #ifdef MUST_ALIGN
  895.  
  896. #if 0 /* It turns out we must always make extra space, if MUST_ALIGN
  897.      because we must always round the address up at the end,
  898.      because we don't know whether the dynamic offset
  899.      will mess up the desired alignment.  */
  900.   /* If we have to round the address up regardless of known_align,
  901.      make extra space regardless, also.  */
  902.   if (known_align % BIGGEST_ALIGNMENT != 0)
  903. #endif
  904.     {
  905.       if (GET_CODE (size) == CONST_INT)
  906.     size = GEN_INT (INTVAL (size)
  907.             + (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1));
  908.       else
  909.     size = expand_binop (DPmode, add_optab, size,
  910.                  GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
  911.                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
  912.     }
  913.  
  914. #endif
  915.  
  916. #ifdef SETJMP_VIA_SAVE_AREA
  917.   /* If setjmp restores regs from a save area in the stack frame,
  918.      avoid clobbering the reg save area.  Note that the offset of
  919.      virtual_incoming_args_rtx includes the preallocated stack args space.
  920.      It would be no problem to clobber that, but it's on the wrong side
  921.      of the old save area.  */
  922.   {
  923.     rtx dynamic_offset
  924.       = expand_binop (DPmode, sub_optab, virtual_stack_dynamic_rtx,
  925.               stack_pointer_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
  926.     size = expand_binop (DPmode, add_optab, size, dynamic_offset,
  927.              NULL_RTX, 1, OPTAB_LIB_WIDEN);
  928.   }
  929. #endif /* SETJMP_VIA_SAVE_AREA */
  930.  
  931.   /* Round the size to a multiple of the required stack alignment.
  932.      Since the stack if presumed to be rounded before this allocation,
  933.      this will maintain the required alignment.
  934.  
  935.      If the stack grows downward, we could save an insn by subtracting
  936.      SIZE from the stack pointer and then aligning the stack pointer.
  937.      The problem with this is that the stack pointer may be unaligned
  938.      between the execution of the subtraction and alignment insns and
  939.      some machines do not allow this.  Even on those that do, some
  940.      signal handlers malfunction if a signal should occur between those
  941.      insns.  Since this is an extremely rare event, we have no reliable
  942.      way of knowing which systems have this problem.  So we avoid even
  943.      momentarily mis-aligning the stack.  */
  944.  
  945. #ifdef STACK_BOUNDARY
  946.   /* If we added a variable amount to SIZE,
  947.      we can no longer assume it is aligned.  */
  948. #if !defined (SETJMP_VIA_SAVE_AREA) && !defined (MUST_ALIGN)
  949.   if (known_align % STACK_BOUNDARY != 0)
  950. #endif
  951.     size = round_push (size);
  952. #endif
  953.  
  954.   do_pending_stack_adjust ();
  955.  
  956.   /* Don't use a TARGET that isn't a pseudo.  */
  957.   if (target == 0 || GET_CODE (target) != REG
  958.       || REGNO (target) < FIRST_PSEUDO_REGISTER)
  959.     target = gen_reg_rtx (DPmode);
  960.  
  961.   mark_reg_pointer (target);
  962.  
  963. #ifndef STACK_GROWS_DOWNWARD
  964.   emit_move_insn (target, virtual_stack_dynamic_rtx);
  965. #endif
  966.  
  967.   /* Perform the required allocation from the stack.  Some systems do
  968.      this differently than simply incrementing/decrementing from the
  969.      stack pointer.  */
  970. #ifdef HAVE_allocate_stack
  971.   if (HAVE_allocate_stack)
  972.     {
  973.       enum machine_mode mode
  974.     = insn_operand_mode[(int) CODE_FOR_allocate_stack][0];
  975.  
  976.       if (insn_operand_predicate[(int) CODE_FOR_allocate_stack][0]
  977.       && ! ((*insn_operand_predicate[(int) CODE_FOR_allocate_stack][0])
  978.         (size, mode)))
  979.     size = copy_to_mode_reg (mode, size);
  980.  
  981.       emit_insn (gen_allocate_stack (size));
  982.     }
  983.   else
  984. #endif
  985.     anti_adjust_stack (size);
  986.  
  987. #ifdef STACK_GROWS_DOWNWARD
  988.   emit_move_insn (target, virtual_stack_dynamic_rtx);
  989. #endif
  990.  
  991. #ifdef MUST_ALIGN
  992. #if 0  /* Even if we know the stack pointer has enough alignment,
  993.       there's no way to tell whether virtual_stack_dynamic_rtx shares that
  994.       alignment, so we still need to round the address up.  */
  995.   if (known_align % BIGGEST_ALIGNMENT != 0)
  996. #endif
  997.     {
  998.       target = expand_divmod (0, CEIL_DIV_EXPR, DPmode, target,
  999.                   GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
  1000.                   NULL_RTX, 1);
  1001.  
  1002.       target = expand_mult (DPmode, target,
  1003.                 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
  1004.                 NULL_RTX, 1);
  1005.     }
  1006. #endif
  1007.   
  1008.   /* Some systems require a particular insn to refer to the stack
  1009.      to make the pages exist.  */
  1010. #ifdef HAVE_probe
  1011.   if (HAVE_probe)
  1012.     emit_insn (gen_probe ());
  1013. #endif
  1014.  
  1015.   return target;
  1016. }
  1017.  
  1018. /* Return an rtx representing the register or memory location
  1019.    in which a scalar value of data type VALTYPE
  1020.    was returned by a function call to function FUNC.
  1021.    FUNC is a FUNCTION_DECL node if the precise function is known,
  1022.    otherwise 0.  */
  1023.  
  1024. rtx
  1025. hard_function_value (valtype, func)
  1026.      tree valtype;
  1027.      tree func;
  1028. {
  1029.   return FUNCTION_VALUE (valtype, func);
  1030. }
  1031.  
  1032. /* Return an rtx representing the register or memory location
  1033.    in which a scalar value of mode MODE was returned by a library call.  */
  1034.  
  1035. rtx
  1036. hard_libcall_value (mode)
  1037.      enum machine_mode mode;
  1038. {
  1039.   return LIBCALL_VALUE (mode);
  1040. }
  1041.